package com.entrogy.service.impl;

import com.entrogy.bean.WeixinMenuEntity;
import com.entrogy.bean.WeixinTypeEntity;
import com.entrogy.dao.WeixinMenuMapper;
import com.entrogy.service.WeixinMenuService;
import com.entrogy.utils.RedisUtil;
import com.entrogy.utils.StrUtils;
import com.entrogy.utils.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 存入缓存的时候分 德育处-----5，生活老师----3，家长---1，保安-----6，普通老师---2，访客----4
 * 6中缓存类型列表
 */
@Service
@Transactional
public class WeixinMenuServiceImpl implements WeixinMenuService {
    @Autowired
    private WeixinMenuMapper weixinMenuMapper;
    private RedisUtil redisUtil = new RedisUtil();
    @Override
    public int insertWeixinMenu(WeixinMenuEntity weixinMenuEntity) {
        //删除缓存
        delMenuCache(weixinMenuEntity.getSchoolCode());
        //插入DB
        int code = weixinMenuMapper.insertWeixinMenu(weixinMenuEntity);
        //放入缓存
        saveMenuCache(weixinMenuEntity.getSchoolCode());
        return code;
    }

    @Override
    public int updateWeixinMenuById(WeixinMenuEntity weixinMenuEntity) {
        //删除缓存
        delMenuCache(weixinMenuEntity.getSchoolCode());
        int code = weixinMenuMapper.updateWeixinMenuById(weixinMenuEntity);
        //存入缓存
        saveMenuCache(weixinMenuEntity.getSchoolCode());
        return code;
    }

    @Override
    public int deleteWeixinMenuById(int id) {
        String schoolCodeDB = weixinMenuMapper.getSchoolCodeById(id);
        //删除缓存
        delMenuCache(schoolCodeDB);
        int code = weixinMenuMapper.deleteWeixinMenuById(id);
        //存入缓存
        saveMenuCache(schoolCodeDB);
        return code;
    }

    @Override
    public List<WeixinMenuEntity> getWeixinMenus() {
        return weixinMenuMapper.getWeixinMenus();
    }

    @Override
    public List<WeixinTypeEntity> getWeixinTypeList() {
        return weixinMenuMapper.getWeixinTypeList();
    }

    @Override
    public void insertWeixinMenuToSchool(String schoolCode) {
        weixinMenuMapper.insertWeixinMenuToSchool(schoolCode);
        //存入缓存
        saveMenuCache(schoolCode);
    }

    private void saveMenuCache(String schoolCode) {

        if(StrUtils.isNullOrEmpty(schoolCode)){
            List<WeixinMenuEntity> parentMenu = weixinMenuMapper.getWeixinMenuList("1");
            List<WeixinMenuEntity> teacherMenu = weixinMenuMapper.getWeixinMenuList("2");
            List<WeixinMenuEntity> lifeTeacherMenu = weixinMenuMapper.getWeixinMenuList("3");
            List<WeixinMenuEntity> deYuChuMenu = weixinMenuMapper.getWeixinMenuList("5");
            List<WeixinMenuEntity> secrityMenu = weixinMenuMapper.getWeixinMenuList("6");
            redisUtil.set(Constants.PARENT_MENU,parentMenu);
            redisUtil.set(Constants.TEACHER_MENU,teacherMenu);
            redisUtil.set(Constants.LIFE_TEACHER_MENU,lifeTeacherMenu);
            redisUtil.set(Constants.DE_YU_CHU_MENU,deYuChuMenu);
            redisUtil.set(Constants.SECRITY_MENU,secrityMenu);
        }else{
            List<WeixinMenuEntity> parentMenu = weixinMenuMapper.getWeixinMenuListByType("1",schoolCode);
            List<WeixinMenuEntity> teacherMenu = weixinMenuMapper.getWeixinMenuListByType("2",schoolCode);
            List<WeixinMenuEntity> lifeTeacherMenu = weixinMenuMapper.getWeixinMenuListByType("3",schoolCode);
            List<WeixinMenuEntity> deYuChuMenu = weixinMenuMapper.getWeixinMenuListByType("5",schoolCode);
            List<WeixinMenuEntity> secrityMenu = weixinMenuMapper.getWeixinMenuListByType("6",schoolCode);
            redisUtil.set(Constants.PARENT_MENU+"_"+schoolCode,parentMenu);
            redisUtil.set(Constants.TEACHER_MENU+"_"+schoolCode,teacherMenu);
            redisUtil.set(Constants.LIFE_TEACHER_MENU+"_"+schoolCode,lifeTeacherMenu);
            redisUtil.set(Constants.DE_YU_CHU_MENU+"_"+schoolCode,deYuChuMenu);
            redisUtil.set(Constants.SECRITY_MENU+"_"+schoolCode,secrityMenu);
        }
        List<WeixinMenuEntity> visitorMenu = weixinMenuMapper.getWeixinMenuListByType("4",schoolCode);
        redisUtil.set(Constants.VISITOR_MENU,visitorMenu);

    }

    private void delMenuCache(String schoolCode) {
        if(StrUtils.isNullOrEmpty(schoolCode)){
            redisUtil.del(Constants.DE_YU_CHU_MENU);
            redisUtil.del(Constants.LIFE_TEACHER_MENU);
            redisUtil.del(Constants.PARENT_MENU);
            redisUtil.del(Constants.SECRITY_MENU);
            redisUtil.del(Constants.TEACHER_MENU);
        }else {
            redisUtil.del(Constants.DE_YU_CHU_MENU+"_"+schoolCode);
            redisUtil.del(Constants.LIFE_TEACHER_MENU+"_"+schoolCode);
            redisUtil.del(Constants.PARENT_MENU+"_"+schoolCode);
            redisUtil.del(Constants.SECRITY_MENU+"_"+schoolCode);
            redisUtil.del(Constants.TEACHER_MENU+"_"+schoolCode);
        }
        redisUtil.del(Constants.VISITOR_MENU);
    }

}
